package com.lzx.iteam.net;

import android.content.Context;
import android.os.Message;
import android.util.Log;
import com.lzx.iteam.bean.WeeklyDetailListData;
import com.lzx.iteam.provider.CloudDBOperation;
import com.lzx.iteam.util.Constants;
import com.lzx.iteam.util.PreferenceUtil;
import com.lzx.iteam.util.StringUtil;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Megan on 16/5/25.
 */
public class WeeklyDetailMsg extends CcMsgStructure {

    private static final String TAG = "WeeklyMyListMsg";

    private Message mmCallback;
    private Context mContext;
    private String mGroupId;


    public WeeklyDetailMsg(Message callback) {
        mmCallback = callback;

    }

    public WeeklyDetailMsg(Message callback, Context context, String groupId) {
        mmCallback = callback;
        mContext = context;
        mGroupId = groupId;

    }

    @Override
    public void onReceive(JSONObject data) {
        WeeklyDetailListData weeklyDetailListData = new WeeklyDetailListData();
        try {
            JSONObject jsonObject = data.getJSONObject(AsynHttpClient.KEY_CC_DATA);
            JSONObject item = jsonObject.getJSONObject("detail");
            weeklyDetailListData.groupId = mGroupId;
            if (item.has("weekly_year")) {
                weeklyDetailListData.weeklyYear = item.getString("weekly_year");
            }
            if (item.has("weekly_month")) {
                weeklyDetailListData.weeklyMonth = item.getString("weekly_month");
            }
            if (item.has("week_count")) {
                weeklyDetailListData.weeklyCount = item.getString("week_count");
            }
            if (item.has("user_id")) {
                weeklyDetailListData.userId = item.getString("user_id");
            }
            if (item.has("user_name")) {
                weeklyDetailListData.userName = item.getString("user_name");
            }

            if (item.has("user_img")) {
                weeklyDetailListData.userImg = item.getString("user_img");
            }

            weeklyDetailListData.createTime = item.getString("create_time");
            weeklyDetailListData.weeklyId = item.getString("_id");
            weeklyDetailListData.content = item.getJSONArray("content").toString();
            if (item.has("comments")) {
                ArrayList<Map<String, String>> commentDatas = new ArrayList<Map<String, String>>();
                JSONArray comments = item.getJSONArray("comments");
                for (int j = 0; j < comments.length(); j++) {
                    JSONObject commentObject = comments.getJSONObject(j);
                    Map<String, String> comment = new HashMap<String, String>();
                    comment.put(Constants.COMMENT_CONTENT, commentObject.getString("comment_content"));
                    comment.put(Constants.COMMENT_CREATE_TIME, commentObject.getString("comment_create_time"));
                    comment.put(Constants.COMMENT_USER_ID, commentObject.getString("comment_user_id"));
                    comment.put(Constants.COMMENT_USER_NAME, commentObject.getString("comment_user_name"));
                    commentDatas.add(comment);
                }
                weeklyDetailListData.comments = commentDatas;
            }
        } catch (JSONException e) {
            Log.d(TAG, "JSONException : " + e.toString());
            e.printStackTrace();
        }

        if (mmCallback != null)

        {
            mmCallback.obj = weeklyDetailListData;
            mmCallback.arg1 = 0;
            mmCallback.sendToTarget();
            return;
        }

    }

    @Override
    public void onError(int error, Object ret) {
        Log.d(TAG, "onError : " + error + " ret: " + ret);
        String errorMsg = null;
        if (ret != null) {
            JSONObject jData = null;
            try {
                jData = new JSONObject(ret.toString());
                errorMsg = jData.getString("msg");
            } catch (JSONException e) {
                errorMsg = "服务端数据解析失败!";
                e.printStackTrace();
            }

        }
        if (mmCallback != null) {
            mmCallback.arg1 = error;
            if (!StringUtil.isEmpty(errorMsg)) {
                mmCallback.obj = errorMsg;
            } else {
                mmCallback.obj = Constants.NETWORK_WRROR;
            }
            mmCallback.sendToTarget();
        }

    }
}
